package com.ruoyi.caipiao.utils;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 第一次随机20万注号码， 去重后剩余8万多注
 * 第二次随机8万多的两倍次，从8万多注种获取号码，得到7万多注
 * 重复第二步， 每次都随机号码个数的2倍次， 然后去重
 * 直到不中当前最新号码
 *
 * @author wangcen
 * @date 2023/12/9 13:59
 */
@Slf4j
public class Pl3NumUtil {

    public static ThreadLocal<String[]> BASE_NUMS = new ThreadLocal<>();

    //    static String str = "001,003,004,005,007,008,012,013,014,015,018,019,021,023,025,027,028,029,030,031,032,034,035,036,037,038,039,041,042,043,044,045,046,047,048,053,055,056,059,060,061,062,063,064,065,066,067,069,070,072,073,075,076,078,079,080,081,082,084,085,088,090,091,092,093,094,095,096,098,102,103,104,105,106,108,110,111,112,113,114,115,116,118,119,120,121,123,124,125,126,128,129,131,132,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,150,152,153,154,155,156,158,163,165,167,169,170,171,172,173,176,177,178,180,181,182,183,184,185,188,189,190,191,193,195,196,197,198,199,200,201,202,204,205,206,207,208,209,210,212,213,214,215,216,217,218,220,223,224,226,227,228,229,230,231,232,234,235,236,237,238,239,241,242,244,245,247,250,251,252,253,254,255,256,257,258,260,261,262,264,265,266,267,268,269,270,271,272,273,274,275,276,277,279,280,281,283,284,286,287,288,290,291,292,293,295,297,298,300,301,305,306,309,310,311,312,314,315,316,318,319,320,321,322,323,324,326,328,329,330,332,334,337,338,340,341,342,343,344,346,347,348,349,350,351,352,353,355,356,357,358,360,361,362,363,365,366,368,369,370,371,373,374,376,377,379,380,381,383,384,386,387,388,389,390,392,394,395,396,397,398,400,401,402,403,404,405,406,407,408,409,411,412,414,415,416,417,419,422,423,426,427,428,429,432,433,435,436,438,439,440,441,442,443,444,445,446,447,448,451,452,453,454,455,456,457,458,460,461,462,463,464,466,467,468,469,470,471,472,473,474,475,476,477,479,481,482,483,484,485,486,487,488,489,490,491,492,494,495,497,498,499,500,501,502,503,504,505,506,507,508,509,512,514,515,517,518,519,521,522,523,524,525,526,527,528,530,531,532,533,535,536,537,539,540,541,544,545,546,547,548,549,551,554,556,557,558,561,562,563,565,566,568,569,570,571,572,573,574,575,577,579,581,582,583,585,586,587,588,590,591,592,594,595,596,599,600,601,602,604,605,606,607,608,609,610,611,612,614,615,617,621,622,623,624,625,626,627,629,631,633,634,635,636,638,640,644,645,646,647,650,651,652,654,655,656,657,658,660,661,662,663,664,665,666,667,668,669,671,672,673,674,675,676,678,679,680,681,683,685,686,687,688,689,690,691,692,693,694,695,696,698,700,701,702,703,704,705,706,707,708,709,710,711,713,714,715,716,717,719,720,721,723,724,726,730,731,732,733,735,736,737,738,740,741,742,744,745,746,747,749,750,751,752,753,755,756,757,758,760,763,764,765,768,769,770,771,773,774,775,776,777,781,783,784,785,786,787,788,789,790,791,792,793,795,796,797,798,799,800,801,803,804,805,806,807,809,810,811,812,813,814,815,816,818,819,820,821,823,824,825,826,828,830,831,832,833,834,835,836,837,838,839,841,842,844,845,846,847,848,851,852,853,854,855,856,858,860,861,862,863,864,866,868,871,872,873,875,876,877,879,881,882,883,884,885,889,891,892,893,894,895,896,897,898,899,900,901,903,905,906,907,908,909,910,911,912,913,914,915,916,917,918,920,921,922,923,924,925,929,930,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,949,951,954,957,958,959,960,961,963,964,966,970,972,975,976,978,979,981,983,984,985,987,988,989,990,991,994,996,997,998,999";
//    static String str = "000,001,002,003,004,005,006,007,008,009,011,012,014,016,017,019,020,022,023,024,025,026,027,028,029,030,031,032,033,034,036,037,038,039,040,041,042,043,045,046,047,049,050,051,052,053,054,055,056,057,058,059,060,062,063,064,065,067,069,070,071,072,073,074,075,077,079,080,081,082,083,084,086,088,089,090,092,093,095,096,098,100,101,102,105,107,108,109,111,112,113,114,115,116,117,118,119,120,121,122,123,124,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,153,154,157,158,159,160,162,164,165,166,169,171,172,173,174,175,177,178,179,180,181,182,183,185,186,188,189,190,191,192,193,194,195,196,197,198,200,201,202,203,205,207,208,209,210,211,212,214,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,236,239,241,243,244,245,246,247,248,249,250,252,253,254,255,256,257,259,261,262,263,265,267,268,269,270,271,272,273,274,278,279,281,282,283,285,289,290,291,294,295,298,299,300,301,302,303,304,305,307,308,309,310,311,314,315,318,320,321,322,325,326,327,328,330,331,332,333,335,336,337,338,339,340,341,342,343,344,345,346,348,349,350,352,354,356,357,359,360,361,362,363,366,368,369,370,371,372,373,374,376,378,379,380,381,383,385,386,388,389,392,393,396,397,398,399,400,401,402,403,405,407,408,409,410,411,413,414,415,416,417,418,419,422,423,424,425,426,427,428,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,446,447,448,449,450,452,453,454,455,456,458,459,460,461,462,463,464,466,467,468,470,473,474,475,476,477,478,479,480,481,482,483,484,486,488,489,490,491,493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,521,522,523,524,525,526,527,528,529,530,531,532,533,536,538,539,540,541,542,543,544,545,546,547,548,549,550,551,552,553,554,556,560,561,562,563,564,565,566,567,569,570,571,574,575,576,577,578,579,580,581,583,584,585,586,587,589,590,591,592,593,594,595,596,597,599,600,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,627,630,632,633,634,635,636,637,638,639,640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,660,661,662,663,664,665,666,667,668,669,670,671,672,673,674,675,676,677,678,679,680,681,682,683,684,685,686,688,689,690,691,692,693,694,695,696,697,698,701,702,703,704,705,707,708,709,710,711,712,713,714,715,716,718,719,721,722,725,726,727,728,729,730,733,734,735,736,737,738,739,740,741,742,743,744,745,747,748,749,751,752,753,754,756,757,762,763,764,766,768,769,770,771,772,773,774,775,776,777,778,779,781,782,783,784,785,787,788,789,790,791,793,794,795,796,798,799,800,802,803,804,805,807,808,809,810,811,813,814,815,816,817,818,819,822,823,825,827,828,830,831,832,834,836,838,839,841,842,844,845,846,847,848,849,850,852,854,855,857,858,861,862,863,864,865,866,867,868,869,870,871,872,873,874,875,876,877,878,880,882,883,884,886,887,888,889,890,891,893,895,896,897,898,899,900,901,902,903,904,906,907,908,909,910,913,914,915,916,917,918,919,921,922,923,924,926,927,929,932,933,934,935,937,938,939,942,943,944,945,946,947,949,951,953,954,956,957,958,959,960,961,962,963,965,966,968,970,971,974,975,976,977,979,981,982,983,986,987,988,989,990,991,992,993,994,995,996,997,998,999";
//    static String str = "000,001,002,004,006,008,010,011,012,013,014,015,016,017,018,019,020,022,023,024,025,026,027,028,029,030,031,032,033,034,035,036,037,038,039,040,042,044,045,046,047,048,049,051,052,053,054,055,056,057,058,059,060,061,062,063,064,065,066,067,070,071,072,073,075,076,077,078,079,080,081,082,083,086,087,088,090,091,092,094,095,096,097,099,100,101,102,103,104,105,107,108,112,113,114,115,116,117,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,141,142,143,144,146,147,149,150,151,152,153,155,156,157,158,160,161,162,163,164,165,167,168,169,170,171,172,174,175,176,178,179,180,181,183,184,185,186,187,188,189,190,191,192,194,195,197,198,199,200,201,202,203,205,207,208,210,211,214,215,216,217,218,219,220,221,222,223,224,225,227,229,230,232,233,235,236,237,238,240,241,242,243,245,246,248,249,251,253,254,255,256,257,258,259,261,262,263,264,265,267,268,269,270,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,288,289,290,291,292,294,295,297,298,299,300,301,302,303,304,305,306,308,310,312,314,315,316,317,318,319,320,323,324,325,326,327,328,330,332,333,334,335,336,337,338,339,341,342,343,345,347,348,349,350,351,352,353,354,356,357,359,360,361,362,363,365,366,367,368,369,370,371,372,373,374,375,376,377,378,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,406,407,408,410,412,414,415,416,417,418,419,421,422,424,425,426,427,429,430,431,432,433,434,435,436,437,438,439,440,442,443,444,447,448,450,451,452,453,455,457,458,459,460,461,462,464,465,467,468,469,470,472,474,475,476,477,478,479,480,481,482,484,485,486,487,488,489,490,491,493,494,495,496,497,499,501,502,503,504,506,507,508,509,510,511,512,513,514,515,516,517,518,521,522,523,524,526,527,528,529,530,532,534,535,536,537,538,539,540,541,543,544,545,546,548,549,550,551,552,553,554,556,557,558,559,560,563,566,567,568,571,572,573,574,575,576,577,578,579,580,581,582,584,585,586,587,588,589,590,591,592,593,594,595,597,599,600,601,602,603,606,607,609,610,611,614,615,616,617,618,619,620,622,623,624,625,626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,645,646,647,649,650,652,653,654,656,657,659,660,661,662,663,664,665,666,667,668,669,670,671,672,673,674,676,678,679,680,681,683,684,686,687,688,689,690,691,692,693,694,695,696,697,698,700,702,703,704,705,708,709,710,712,713,714,716,717,718,719,720,721,722,724,725,728,729,731,732,733,734,735,736,737,738,739,740,741,742,743,745,747,748,749,750,751,752,753,754,755,756,757,758,759,761,762,763,764,765,766,767,769,771,772,773,774,777,778,779,780,781,784,785,787,788,789,790,791,792,793,794,795,796,797,798,799,800,801,804,805,806,807,808,809,810,812,814,815,816,817,818,819,820,821,824,825,826,827,828,829,830,832,834,836,837,838,839,840,841,842,843,844,845,847,848,849,850,851,852,853,854,855,856,858,860,861,863,864,865,867,868,869,870,872,873,874,875,876,879,880,881,882,883,884,885,886,887,888,889,890,891,892,893,895,896,898,899,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,919,920,921,922,923,924,926,927,929,930,931,932,933,934,936,937,938,939,940,941,942,943,945,946,947,948,949,950,951,952,953,954,956,957,958,959,960,961,962,963,964,965,966,967,969,971,972,974,975,976,978,980,981,982,983,985,987,988,989,990,991,992,994,995,996,997,998,999";
//    static String[] strArr = str.split(",");
    static DecimalFormat df = new DecimalFormat();

    static {
        df.applyPattern("000");
    }

    public static String getCurrentMethod() {
        StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[2]; // 通过线程堆栈信息获取当前方法所在位置
        return stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName(); // 返回类名加上方法名作为结果
    }

    /**
     * 随机获取size注号码
     *
     * @param size
     * @return
     */
    public static Set<String> getNums2(int size) {
        Set<String> set = new TreeSet<>();
        for (; ; ) {
            set.add(getNum());
            if (set.size() == size) {
                return set;
            }
        }
    }

    /**
     * 随机获取count次号码，去重得到号码返回
     *
     * @param count
     * @return
     */
    public static Set<String> getNums(int count) {
        Set<String> set = new TreeSet<>();
        for (int i = 0; i < count; i++) {
            set.add(getNum());
        }
        return set;
    }

    /**
     * 获取排列3反集
     *
     * @return
     */
    public static Set<String> getFj(Set<String> set) {
        Set<String> fj = new TreeSet<>();
        for (int i = 0; i < 1000; i++) {
            String ff = df.format(i);
            if (!set.contains(ff)) {
                fj.add(ff);
            }
        }
        return fj;
    }

    public static String getNum() {
        if (Objects.isNull(Pl3NumUtil.BASE_NUMS.get())
                || Pl3NumUtil.BASE_NUMS.get().length == 0) {
            return df.format(ThreadLocalRandom.current().nextInt(1000));
        } else {
            String[] strArr = Pl3NumUtil.BASE_NUMS.get();
            return strArr[ThreadLocalRandom.current().nextInt(strArr.length)];
        }
    }

    /**
     * 获取最新几期号码
     *
     * @param restTemplate
     * @param qs
     * @return
     */
    public static List<String> getLastNums(RestTemplate restTemplate, int qs) {
        List<String> result = new ArrayList<>();
        ResponseEntity<Map> responseEntity = restTemplate.getForEntity("https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=350133&provinceId=0&isVerify=1&termLimits=" + qs, Map.class);
//        System.out.println(responseEntity.getBody());
        if (responseEntity.getStatusCodeValue() == 200) {
            LinkedHashMap<String, Object> value = (LinkedHashMap<String, Object>) responseEntity.getBody().get("value");
            List<LinkedHashMap> list = (List) value.get("list");
            for (LinkedHashMap lm : list) {
                String v = (String) lm.get("lotteryUnsortDrawresult");
                result.add(v.replaceAll(" ", "").substring(0, 3));
            }
        }
        return result;
    }

    /**
     * 获取最新几期号码
     *
     * @param restTemplate
     * @param qs
     * @return
     */
    public static Map<String, String> getLastNumsMap(RestTemplate restTemplate, int qs) {
        Map<String, String> result = new TreeMap<>();
        ResponseEntity<Map> responseEntity = restTemplate.getForEntity("https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=350133&provinceId=0&isVerify=1&termLimits=" + qs, Map.class);
        if (responseEntity.getStatusCodeValue() == 200) {
            LinkedHashMap<String, Object> value = (LinkedHashMap<String, Object>) responseEntity.getBody().get("value");
            List<LinkedHashMap> list = (List) value.get("list");
            for (LinkedHashMap lm : list) {
                String date = (String) lm.get("lotteryDrawTime");
                String v = (String) lm.get("lotteryUnsortDrawresult");
                result.put(date, v.replaceAll(" ", "").substring(0, 3));
            }
        }
        return result;
    }

    /**
     * 字符串转set
     *
     * @param str
     * @return
     */
    public static Set<String> str2Set(String str) {
        String[] split = str.replace("]", "").replaceAll("\n", "")
                .replace("[", "").replaceAll(" ", "").split(",");
        Set<String> set = new TreeSet<>();
        for (String s : split) {
            set.add(s.trim());
        }
        return set;
    }

    /**
     * set -> 字符串
     *
     * @return
     */
    public static String set2Str(Set<String> set) {
        return set.toString().replaceAll(" ", "")
                .replaceAll("]", "")
                .replaceAll("\n", "")
                .replaceAll("\\[", "")
                .replaceAll(" ", "");
    }

    /**
     * 两两交集再并集
     *
     * @param list
     * @return
     */
    public static Set<String> lljj(List<Set<String>> list) {
        Set<String> bj = new TreeSet<>();
        int size = list.size();
        for (int i = 0; i < size - 1; i++) {
            Set<String> s1 = list.get(i);
            for (int j = i + 1; j < size; j++) {
                Set<String> s2 = list.get(j);
                s2 = new TreeSet<>(s2);
                s2.retainAll(s1);
                bj.addAll(s2);
            }
        }
        return bj;
    }

    public static Set<String> positionNum2Code(String p1, String p2, String p3) {
        Set<String> set = new TreeSet<>();
        String[] split1 = p1.split(",");
        String[] split2 = p2.split(",");
        String[] split3 = p3.split(",");
        for (String s1 : split1) {
            for (String s2 : split2) {
                for (String s3 : split3) {
                    set.add(s1 + s2 + s3);
                }
            }
        }

        return set;
    }


    /**
     * 随机生成号码：此随机一注号码，第一层随机号码的交集，剩余一注
     */
    public static String getRandomNum(int size) {
        if (size > 999) {
            throw new IllegalArgumentException("size不能大于999");
        }
        Set<String> nums = getNums(size);
        for (; ; ) {
            nums.retainAll(getNums(size));
            if (nums.size() == 1) {
                return nums.iterator().next();
            }
            if (nums.isEmpty()) {
                nums = getNums(size);
            }
        }
    }

    public static void main(String[] args) {
//        Map<String, String> lastNumsMap = Pl3NumUtil.getLastNumsMap(new RestTemplate(), 365);
//        System.out.println(lastNumsMap.toString());

        String lastNum = "203";
        int size = 900;

        /**
         * 生成号码
         */
        for (int j = 0; j < 300000; j++) {
            Set<String> set = new TreeSet<>();
            for (int i = 0; i < 5; i++) {
                String randomNum = getRandomNum(size);
                set.add(randomNum);
            }
            if (set.isEmpty()) {
                continue;
            }
            System.out.println(j + "-" + set.size() + ":" + set.contains(lastNum));
            MyFileUtil.writeContentToFile("D:\\方法6\\" + DateUtil.format(new Date(), "yyyyMMdd") + ".txt", Pl3NumUtil.set2Str(set) + "\n", true);
        }

        /**
         * 得到投注号码
         */
        List<String> strings = MyFileUtil.readFileAsList(new File("D:\\方法6\\20250624_" + size + ".txt"));
        Set<String> ss = new TreeSet<>();
        Set<String> jj = new TreeSet<>();
        for (int i = 0; i < strings.size(); i++) {
            String s = strings.get(i);
            if (StringUtils.isNotBlank(s.trim())) {
                ss.addAll(Pl3NumUtil.str2Set(s));
                if (i > 0 && i % 200 == 0) {
                    System.out.println(i + "---->" + ss.size() + "--" + ss.contains(lastNum));
                    if (jj.isEmpty()) {
                        jj.addAll(ss);
                    } else {
                        jj.retainAll(ss);
                    }
                    ss = new TreeSet<>();
                }
            }
            if (i > 0 && i % 200 == 0) {
                System.out.println("size:" + jj.size() + "," + jj.contains(lastNum));
                if (!jj.contains(lastNum)) {
                    break;
                }
            }
        }
        System.out.println("size:" + jj.size() + "," + jj.contains(lastNum));
    }

}
